package com.dz.risk.service.impl;

import com.dz.risk.common.utils.DateUtils;
import com.dz.risk.constant.IndexConstant;
import com.dz.risk.pojo.domain.ReportFormsDataValueEntity;
import com.dz.risk.pojo.domain.TemporaryTableOfDataEntity;
import com.dz.risk.service.IndexDealWith;
import com.dz.risk.service.TemporaryTableOfDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 人工导入类型编码数据处理类
 *
 * @author Doke
 * @date 2021/9/7 13:20
 */
@Service("IndexDealWith_" + IndexConstant.MANUAL_IMPORT)
public class IndexDealWithTwoImpl implements IndexDealWith {

    private final TemporaryTableOfDataService temporaryTableOfDataService;

    @Autowired
    public IndexDealWithTwoImpl(TemporaryTableOfDataService temporaryTableOfDataService) {
        this.temporaryTableOfDataService = temporaryTableOfDataService;
    }

    /**
     * 根据报表单元格信息和计算时间获取指标数据
     *
     * @param reportFormsDataValueEntity 报表单元格信息
     * @param calculatingTime            计算版本
     * @param beginDate                  报表开始时间
     * @param endDate                    报表结束时间
     * @return 数据值
     */
    @Override
    public Double getDataByReportFormsDataValueAndCalculatingTime(ReportFormsDataValueEntity reportFormsDataValueEntity, Date calculatingTime, Date beginDate, Date endDate) {
        Double indexValue = null;

        //查询中间临时表是否有改计算月份得数据
        List<TemporaryTableOfDataEntity> temporaryTableOfDataEntities = temporaryTableOfDataService.selectByIndexIdAndDate(reportFormsDataValueEntity.getTableCellSerialNumber(), DateUtils.dateToLongYears(calculatingTime));
        //如果有则取第一条得指标值
        if (!CollectionUtils.isEmpty(temporaryTableOfDataEntities)) {
            indexValue = Double.parseDouble(temporaryTableOfDataEntities.get(0).getIndexValue());
        }
        return indexValue;
    }
}
